import Flutter
import UIKit

public class SwiftFlutterImmersionBarPlugin: NSObject, FlutterPlugin {
  public static func register(with registrar: FlutterPluginRegistrar) {
    let channel = FlutterMethodChannel(name: "vip.hsq168.plugin.flutter_immersion_bar", binaryMessenger: registrar.messenger())
    let instance = SwiftFlutterImmersionBarPlugin()
    registrar.addMethodCallDelegate(instance, channel: channel)
    
  }

    
   
    
  public func handle(_ call: FlutterMethodCall, result: @escaping FlutterResult) {
    //result("iOS " + UIDevice.current.systemVersion)
     var statusBarUIView: UIView? {
              if #available(iOS 13.0, *) {
                  let tag = 38482458385
                  if let statusBar = UIApplication.shared.keyWindow?.viewWithTag(tag) {
                      return statusBar
                  } else {
                      let statusBarView = UIView(frame: UIApplication.shared.statusBarFrame)
                      statusBarView.tag = tag 
                      UIApplication.shared.keyWindow?.addSubview(statusBarView)
                      return statusBarView
                  }
              } else {
               return UIApplication.shared.value(forKeyPath: "statusBarWindow.statusBar") as? UIView
              }
              return nil
          }
    if(statusBarUIView == nil) {
        result(false)
        return
    }
    if(call.method=="statusBarAlpha"){
       statusBarUIView!.backgroundColor=statusBarUIView!.backgroundColor?.withAlphaComponent((call.arguments as! CGFloat))
    }
     if(call.method=="statusBarColor"){
        let rgbValue=call.arguments as! Int
        let redValue=((rgbValue & 0xFF0000) >> 16)
        let greenValue=((rgbValue & 0xFF00) >> 8)
        let blueValue=(rgbValue & 0xFF)
        let maxValue:Float=255;
        let color=UIColor(_colorLiteralRed: (Float(redValue))/maxValue,green: (Float(greenValue))/maxValue, blue: (Float(blueValue))/maxValue, alpha: 1)
        statusBarUIView!.backgroundColor=color;
    }
    if(call.method=="statusBarDarkFont"){
        let agrs=call.arguments as! Array<Any>;
        let isDarkFont=agrs[0] as! Bool 
        if #available(iOS 13.0, *) {
            UIApplication.shared.setStatusBarStyle((isDarkFont ? UIStatusBarStyle.darkContent : UIStatusBarStyle.lightContent), animated: false)
        } else {
            UIApplication.shared.setStatusBarStyle((isDarkFont ? UIStatusBarStyle.default : UIStatusBarStyle.lightContent), animated: false)
        }
    }
    result(true)
   // statusBar.backgroundColor =  UIColor.black
  }
  
    
}
